package main

/*
 * @lc app=leetcode.cn id=123 lang=golang
 *
 * [123] 买卖股票的最佳时机 III
 */

// @lc code=start
func maxProfit(prices []int) int {
	if len(prices) <= 1 {
		return 0
	}
	buy1 := -prices[0]
	buy2 := -prices[0]
	profit1 := 0
	profit2 := 0
	for _, price := range prices {

		buy1 = max(buy1, -price)
		profit1 = max(profit1, price+buy1)
		buy2 = max(buy2, profit1-price)
		profit2 = max(profit2, price+buy2)
	}

	return profit2
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// @lc code=end
